<?php
require_once('Model/SiteManage.php');
require_once('Model/DB.php');
require_once('Model/Validate.php');
class Model_Banner
{
    var $sys;
    var $config;
    private $table_name = "D_BANNER";
    
    public function __construct()
    {
        $this->sys = new Model_SiteManage();
        $config       = __get_config('plugin.ini');
        $this->config = $config['MANAGE_BANNER'];
    }

    public function insert($data)
    {
        $db = new Model_DB();
        return $db->insert($this->table_name, $data);
    }
    public function update($id, $data)
    {
        $db = new Model_DB();
        return $db->update($id, $this->table_name, $data);
    }
    public function get($id)
    {
        $db = new Model_DB();
        $sql = sprintf("select * from ".$this->table_name." where id = " . $db->escape($id));
        $db->query($sql);
        return $db->fetch();
    }
    public function delete($id)
    {
        $db   = new Model_DB();
        $data = array();
        $data['id'] = $id;
        return $db->delete($this->table_name, $data);
    }
    public function getListCount($data)
    {
        $db    = new Model_DB();
        $where = $this->createSearchCondition($data);
        $sql   = "select count(*) from ".$this->table_name;
        $sql  .= (!empty($where) ? " where ".implode(' AND ', $where) : "");
        $db->query($sql);
        if ($db->res){
            return array_shift($res = $db->fetch());
        }
        return false;
    }
    public function getList($data, $orderby = 'id asc', $limit = '', $offset = '')
    {//var_dump($data);exit;
        $list  = array();
        $db    = new Model_DB();
        $where = $this->createSearchCondition($data);
        $sql   = "select * from ".$this->table_name;
        $sql  .= (!empty($where) ? " where ".implode(' AND ', $where) : "") . (!empty($orderby) ? " order by {$orderby}" : "") . (!empty($limit) ? " limit {$limit}" : "") . (!empty($offset) ? " offset {$offset}" : "");
//echo $sql;exit;
        $db->query($sql);
        if ($db->res){
            while($res = $db->fetch()){
                if(isset($res['id'])){
                    $list[$res['id']] = $res;
                }
                else{
                    $list[] = $res;
                }
            }
        }
        return $list;
    }
    private function createSearchCondition($data, $prefix = '')
    {
        $db    = new Model_DB();
        $where = array();
        if ($prefix != ''){
            $prefix = $prefix . '.';
        }
        $where[] = "{$prefic}site = {$this->sys->getCurrentSite()}";
        foreach($db->getFieldList($this->table_name) as $key => $_){
            if (isset($data[$key]) && $data[$key] !== ''){

                if(is_array($data[$key]))//データが配列の場合
                {
                    foreach($data[$key] as $one)
                    {
                        $value = $db->escape($one);
                        $tmp[$key][] = "{$prefix}{$key} = '{$value}'";
                    }
                    $where[] = implode(" OR ", $tmp[$key]);
                }
                else
                {
                    $value = $db->escape($data[$key]);
                    if ($key == 'title'){
                        $where[] = "{$prefix}{$key} collate utf8_unicode_ci like '%{$value}%'";
                    }else{
                        $where[] = "{$prefix}{$key} = '{$value}'";
                    }
                }
            }                
        }
        
        return $where;
    }
    
    public function validate(&$data, &$err)
    {
        $validate = new Model_Validate();
        // required
        $keys = explode(',', 'status');
        foreach($keys as $key){
            if (!isset($data[$key]) || $validate->NotEmpty($data[$key])){
                $err[$key]['NotEmpty'] = true;
            }
        }
        
        return empty($err);
    }
    
    //　状態
    public function getStatusMap()
    {
        $map    = array();
        $map[2] = '仮登録';
        $map[1] = '公開';
        $map[0] = '非公開';
        return $map;
    }
    // 
    public function getTargetMap()
    {
        $map    = array(0 => '同ウインドウ', 1 => '別ウインドウ' );
        return $map;
    }
    public function getAnimationMap()
    {
        $map    = array(1 => '通常', 2 => 'はみだす', 3 => 'バウンド' );
        return $map;
    }
    //　時間
    public function getTimeMap()
    {
        for($i=0; $i<24; $i++)
        {
            $map[$i] = $i;
        }
        return $map;
    }
    
    // テンプレート（ページ管理）
    public function getTemplateMap()
    {
        $map = array('company/index.html' => '会社情報');
        return $map;
    }
    
    public function getFormKeys()
    {
        $db = new Model_DB();
        return array_keys($db->getFieldList($this->table_name));
    }
    
    public function getSiteUrl()
    {
        $site_dir = $_SESSION['DASH_BOARD']['SITE_DIR'];
        return 'http://' . $_SERVER['HTTP_HOST'] . '/' . $site_dir . '/';
    }
    public function getSiteDir($mkdir = false)
    {
        $site_dir = $_SESSION['DASH_BOARD']['SITE_DIR'];
return $site_dir;
/*
        if ($mkdir == true){
            chdir(SiteManageRootDir . '/htdocs/seisansha/');
            if (!file_exists($site_dir)){
                mkdir($site_dir);
                chmod($site_dir, 0777);
            }
        }
        return SiteManageRootDir . '/htdocs/' . $site_dir . '/';
*/
    }
}
?>